package com.xvheng.web.controller.monitor;

import com.xvheng.common.core.controller.BaseController;
import com.xvheng.common.core.page.TableDataInfo;
import com.xvheng.system.domain.SysOutlog;
import com.xvheng.system.service.SysOutputLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;


/**
 * 系统后台打印日志类
 * @className: SysOutputLogCtroller
 * @description:TODO
 * @author: dlq1751087535@163.com
 * @date: 2020年5月11日 上午10:53:10
 */

@CrossOrigin
@Controller
@RequestMapping("/monitor/sysoutputlog")
public class SysOutputLogCtroller extends BaseController {

    @Value("${xvheng.loghome}")
    private String syslogpath;
    @Autowired
    private SysOutputLogService sysOutputLogServiceImpl;
    /**
     *
     * @title: getsyslog
     * @description:TODOge
     * @author: dlq1751087535@163.com
     * @return
     * @throws UnsupportedEncodingException
     * @date：2020年5月11日上午9:41:33
     *   修改 ：将当前的递归方法进行去除
     */

    @PreAuthorize("@ss.hasPermi('monitor:sysoutputlog:getsyslog')")
    @PostMapping("/getsyslog")
    @ResponseBody
    public TableDataInfo getsyslog(SysOutlog syslog) throws UnsupportedEncodingException {
        List<SysOutlog> json = sysOutputLogServiceImpl.getsyslog(syslogpath, syslog, "desc");
        TableDataInfo dataTable = getDataTable(json);
        return dataTable;

    }

    /**
     * 文件下载，根据传递的路径，惊醒文件的下载任务
     *
     * @title: getsyslogdown
     * @description:TODO
     * @author: dlq1751087535@163.com
     * @param req
     * @param res
     * @throws FileNotFoundException
     * @throws UnsupportedEncodingException
     * @date：2020年5月11日上午10:50:26
     */
    @RequestMapping("/getsyslogdown")
    public void getsyslogdown(HttpServletRequest req, HttpServletResponse res)
            throws FileNotFoundException, UnsupportedEncodingException {
        String pathstr = req.getParameter("pathstr");
        pathstr = URLDecoder.decode(pathstr, "utf-8");
        sysOutputLogServiceImpl.down(res, pathstr);
    }
}
